home *** CD-ROM | disk | FTP | other *** search
/ Asymetrix Multimedia Toolbook 4.0 (CBT Edition) / Asymetric Multimedia Toolbook 4.0 (CBT Edition).iso / ctb40mt.z / UTILS.ATS < prev    next >
Text File  |  1995-11-13  |  16KB  |  556 lines

  1. SCRIPT "Add scrollbars automatically"
  2. BEHAVIOR "Placed in a field or recordfield, lets the field automatically adjust its scrollbars"
  3. CATEGORY Text
  4. {
  5. --You must send the custom message from somewhere, e.g. at the enterPage event
  6. to handle showOrHideScrollbar
  7.     if my textOverflow > 0 or my scroll > 0
  8.         set my borderStyle to scrolling
  9.     else
  10.         set my borderStyle to rectangle
  11.     end if
  12. end showOrHideScrollbar
  13. }
  14.  
  15. ACTION    "Show Command window"
  16. BEHAVIOR    "This one line script brings up the Command window without using the Command menu item or the Shift + F3 key."
  17. CATEGORY    Debug
  18. {
  19.     show commandWindow
  20. }
  21.  
  22. ACTION    "Beep"
  23. BEHAVIOR    "Beeps the computer once."
  24. CATEGORY    Utilities
  25. {
  26.   beep 1
  27. }
  28.  
  29. SCRIPT    "Check for characters"
  30. BEHAVIOR "This function checks each character to ensure that it is within the bounds of (A-Z) and (a-z)."
  31. CATEGORIES Data
  32. {
  33. to get isStringAlpha testString
  34.     alphabet = "abcdefghijklmnopqrstuvwxyz"
  35.     step i from 1 to charcount(testString)
  36.         if char i of testString is not in alphabet
  37.             return FALSE
  38.         end    if
  39.     end    step
  40.     return TRUE
  41. end isStringAlpha
  42. }
  43.  
  44.  
  45. SCRIPT    "Find the offset of an item"
  46. BEHAVIOR    "A fast way to find the offset of an item in a list."
  47. CATEGORIES     Utilities
  48. {
  49. to get itemOffset itm,lst
  50.     step i from 1 to itemcount(lst)
  51.         pop lst
  52.         if itm = it
  53.             return i
  54.         end    if
  55.     end step
  56.     return 0
  57. end itemOffset
  58. }
  59.  
  60. SCRIPT    "Check a list for an item"
  61. BEHAVIOR    "This function checks to see if an item is in a list."
  62. CATEGORIES     Utilities
  63. {
  64. to get itemInList xitem,xlist
  65.     while itemcount(xlist) > 0 and item 1 of xlist <> xitem
  66.         pop xlist
  67.     end while
  68.     if item 1 of xlist = xitem
  69.         return true
  70.     else
  71.         return false
  72.     end    if
  73. end itemInList
  74. }
  75.  
  76. ACTION    "Select a line of text"
  77. BEHAVIOR    "In a single or multi-select list box, select a line of text."
  78. CATEGORIES    Text
  79. ARG    fieldName     is "textField"    help    "Set this to the name of the field."
  80. ARG    fieldLine     is "3"    help    "Set this to the line you want selected."
  81. {
  82.     set focus to field "$$fieldName"
  83.     set selectedTextLines of field "$$fieldName" to $$fieldLine
  84. }
  85.  
  86. ACTION    "Deselect a line of text"
  87. BEHAVIOR    "Deselects text in a single or multi-select list box."
  88. CATEGORIES    Text
  89. ARG    fieldName    is    "textField"    help    "Set this to the name of the field."
  90. {
  91.     if selectedTextLines of field "$$fieldName" <> null
  92.         set selectedTextLines of field "$$fieldName" to null
  93.     end if
  94. }
  95.  
  96. ACTION    "Put selected text into Command window"
  97. BEHAVIOR    "Puts the currently selected text into the command window"
  98. CATEGORIES     Text
  99. {
  100. -- a good place for this script is in a 
  101. -- buttonDoubleClick in the field itself
  102.     get selectedText 
  103.     put it
  104. }
  105. ACTION    "Request the selected text"
  106. BEHAVIOR    "Puts the text of the selected textlines in a list box into a request box"
  107. CATEGORIES    Text
  108. ARG    fieldName    is    "textField"    help    "Set this to the name of the field."
  109. {
  110.     local temp1,temp2
  111.     set temp2 to null
  112.     get selectedTextLines of field $$fieldName
  113.     put it into temp1
  114.     step i from 1 to itemCount (temp1)
  115.         put textLine (item i of temp1) of text of field $$fieldName & CRLF\
  116.          after temp2
  117.     end step
  118.     request temp2
  119. }
  120.  
  121. ACTION    "Clear the selected text"
  122. BEHAVIOR    "In a list box, clears the selected text. Place this script in a list box"
  123. CATEGORIES    Text
  124. {
  125.     if selectedTextState <> NULL
  126.         set startChar to item 1 of selectedTextState
  127.         set endChar to item 2 of selectedTextSTate
  128.         clear chars startChar to endChar + 2 of text of self  
  129.     end if
  130. }
  131.  
  132. SCRIPT    "Allow normal tabs in a field"
  133. BEHAVIOR    "Allows the user to use the tab normally in a field, rather than having the tab move the user to another object."
  134. CATEGORIES     Text
  135. {
  136. --put this handler in a page or background
  137. to handle keyDown key,isShift,isCtrl
  138.     if key is keytab and isShift is FALSE and isCtrl is FALSE
  139.         send keydown keytab, FALSE, TRUE
  140.     else
  141.         forward
  142.     end if
  143. end    keyDown
  144. }
  145.  
  146. SCRIPT    "Remove a character from a string"
  147. BEHAVIOR    "This function removes all instances of the specified character from the specified string."
  148. CATEGORIES    Data
  149. {
  150. -- removes all instances of chr from strng
  151. to get stripChar chr,strng
  152.     set chrLoc to offset(chr,strng)
  153.     while chrLoc > 0
  154.         clear char chrLoc of strng
  155.         set chrLoc to offset(chr,strng)
  156.     end while
  157.     return strng
  158. end stripChar 
  159. }
  160.  
  161. SCRIPT    "Remove trailing spaces from a string"
  162. BEHAVIOR    "This function removes all trailing spaces, as well as other undesirable characters."
  163. CATEGORIES    Data
  164. {
  165. -- removes trailing spaces, tabs, and crlf's
  166. to get trim strng
  167.     local charsToDrop
  168.     set charsToDrop to space & crlf & tab
  169.     while charcount(strng) > 0 and last char of strng is in charsToDrop
  170.         clear last char of strng
  171.     end    while
  172.     return strng
  173. end trim 
  174. }
  175.  
  176. SCRIPT    "Get the day from the date"
  177. BEHAVIOR    "Returns the weekday of any day beyond 1/1/1700. Requires the function isLeapYear() ('Determine if a Leap Year')."
  178. CATEGORIES    Dates
  179. {
  180. -- returns the weekday of any day beyond 1/1/1700: 
  181. -- note: requires full year. Example: get weekDay("1/1/1993")
  182. -- also requires isLeapYear() function below
  183. to get dayOfWeek pdate
  184.     -- make a list out of the date
  185.     format date pdate as "m,d,y" from "m/d/y"
  186.     set m to item 1 of pdate 
  187.     set d to item 2 of pdate 
  188.     set y to item 3 of pdate 
  189.     -- the following list (1 item for each month) contains the 
  190.     -- number of days that have passed before first day of 
  191.     -- each month.
  192.     set days to "0,31,59,90,120,151,181,212,243,273,304,334" 
  193.     -- set numdays to number of days elapsed since 1/1/1700 
  194.     set numdays to (y-1700) div 4-(y-1700) div 100 +(y-1600) \
  195.      div 400 + 365 * (y - 1700) + item m of days + d -1 
  196.     if isLeapYear(y) and m <= 2 
  197.         decrement numdays by 1 
  198.     end if 
  199.     set n to  (numdays-2) mod 7 +1
  200.     return item n of \
  201.      "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday" 
  202. end dayOfWeek
  203. }
  204.  
  205. SCRIPT    "Get the days in a month"
  206. BEHAVIOR    "Returns the number of days in a month. Requires the function isLeapYear() ('Determine if a Leap Year')."
  207. CATEGORIES Dates
  208. {
  209. -- function: daysInMonth(<month>,<four-digit year>)
  210. -- Returns the number of days in a month.   
  211. --
  212. -- NOTE: this function requires 4 digits for the year.  
  213. -- This function calls and requires the function isLeapYear() defined below.
  214. --
  215. -- example: to find out how many days are in February, 1992 
  216. -- set x to daysInMonth(2,1992) 
  217. --
  218. to get daysInMonth m,y 
  219.     if m <> 2 
  220.         return item m of "31,28,31,30,31,30,31,31,30,31,30,31" 
  221.     else 
  222.         if isLeapYear(y) 
  223.             return 29 
  224.         else 
  225.             return 28 
  226.         end if 
  227.     end if 
  228. end daysInMonth
  229. }
  230.  
  231. SCRIPT "Determine if a leap year"
  232. BEHAVIOR "Returns TRUE if 'y' is a leap year, FALSE if not."
  233. CATEGORIES Dates 
  234. {
  235. -- NOTE: this fuction requires a four digit year.
  236. --
  237. -- Example:  Find out if 1992 is a leap year.
  238. -- if leapYear(1992)
  239. to get isLeapYear y
  240.     if ((y mod 4 = 0 and y mod 100 <> 0) or (y mod 400 = 0))
  241.         return TRUE
  242.     else
  243.         return FALSE
  244.     end
  245. end    isLeapYear
  246. }
  247.  
  248. SCRIPT    "Find a new date"
  249. BEHAVIOR    "Returns a date n days from a given date."
  250. CATEGORIES Dates
  251. {
  252.   -- function: newDate(<original date>,<number of days to add>)
  253. --
  254. -- Returns a date n days from a given date.  Pass it a date 
  255. -- for the first parameter, followed by an integer (positive 
  256. -- or negative) representing the number of days from that date
  257. --
  258. -- Example:  to find out the date 10 days ago
  259. -- set x to newDate(sysdate,-10)
  260. --
  261. to get newDate origdate, days
  262.     format date origdate as "seconds"
  263.     increment origdate by (days*24*60*60)
  264.     format date origdate as sysdateformat from "seconds"
  265.     return origdate
  266. end    newDate
  267. }
  268.  
  269. ACTION "Set Tool palette to non-sticky"
  270. BEHAVIOR "This script gives you the option of reverting to the selection tool after drawing an object instead of staying with the 'draw' tool."
  271. HANDLER     Make
  272. CATEGORIES    Utilities
  273. {
  274. -- This should go at the book level or in a system book
  275.     forward
  276.     set sysTool to "Select"
  277. }
  278.  
  279. ACTION "Show properties dialog on make"
  280. BEHAVIOR "This causes ToolBook to display the properties dialog box of each object as it is made."
  281. HANDLER     Make
  282. CATEGORIES    Utilities
  283. {
  284. -- This should go at the book level or in a system book
  285.     forward
  286.     send properties to target
  287. }
  288.  
  289. SCRIPT    "Get the current path"
  290. BEHAVIOR    "This script gets the current DOS path setting."
  291. CATEGORIES DOS
  292. {
  293. to get findCurrentPath
  294.     --Link to the TB40dos dll
  295.     linkDLL sysToolBookDirectory & "tb40DOS.DLL"
  296.         STRING getDOSEnvironmentString (STRING)
  297.     end linkDLL
  298.  
  299.     --Get the path list 
  300.     set currentPathSetting to  getDOSEnvironmentString ("PATH")
  301.       return currentPathSetting
  302.     unlinkDLL sysToolBookDirectory & "tb40DOS.DLL"
  303. end findCurrentPath   
  304. }
  305.  
  306. SCRIPT "Get path from book name"
  307. BEHAVIOR "Find the path of the book executing the script"
  308. CATEGORY DOS
  309. {
  310. to get bookPath
  311.     pth = name of this book
  312.     while last char of pth <> "\"
  313.         clear last char of pth
  314.     end while
  315.     return pth
  316. end bookPath 
  317. }
  318.  
  319. SCRIPT "Get book name without path"
  320. BEHAVIOR "Find the name of the book without any qualifying path"
  321. CATEGORY DOS
  322. {
  323. to get bookName
  324.     nm = name of this book
  325.     while "\" is in nm
  326.         clear first char of nm
  327.     end while
  328.     return nm
  329. end bookName 
  330. }
  331.  
  332. SCRIPT    "Get the current drive letter"
  333. BEHAVIOR "This function returns the directory letter from a full path after checking for its validity."
  334. CATEGORIES DOS    
  335. {
  336. to get getDriveLetter pathToCheck
  337.  
  338.    if char 2 of pathToCheck is ":"
  339.    --Link to the TB40dos dll and get the current drive list
  340.         linkDLL sysToolBookDirectory & "tb40dos.dll"
  341.             STRING getDriveList(STRING)
  342.         end linkDLL
  343.        
  344.         get getDriveList("")
  345.         unlinkdll sysToolBookDirectory & "tb40dos.dll"
  346.         
  347.         --Make sure that the drive is in the list
  348.         if char 1 of pathToCheck is in it
  349.             return upperCase(char 1 of pathToCheck)
  350.         end if
  351.    end if     
  352.    return NULL
  353. end getDriveLetter
  354. }
  355.  
  356. SCRIPT    "Get free disk space on a drive"
  357. BEHAVIOR "Returns the amount of space available on a  particular drive."
  358. CATEGORIES DOS
  359. {
  360. to get findDiskSpace whatDrive
  361.    local LONG diskSpaceAvailable
  362.       --Link to the TB40dos dll
  363.     linkDLL sysToolBookDirectory & "tb40dos.dll"
  364.         LONG getFreeDiskSpace(STRING)
  365.     end linkDLL
  366.  
  367.     set diskSpaceAvailable to getFreeDiskSpace(whatDrive)
  368.     unlinkDLL sysToolBookDirectory & "tb40dos.dll"
  369.       return diskSpaceAvailable
  370. end findDiskSpace  
  371. }
  372.  
  373. SCRIPT    "Search the path for a file"
  374. BEHAVIOR "This script determines if the file is in the path."
  375. CATEGORIES DOS
  376. {
  377.  to get fileInPath fileNameToCheck
  378.     
  379.     --Link to the TB40dos dll
  380.     linkDLL sysToolBookDirectory & "tb40dos.dll"
  381.         INT fileExists (STRING)
  382.         STRING getDOSEnvironmentString (STRING)
  383.     end linkDLL
  384.     
  385.     --Get the path list 
  386.     set currentPathList to getDOSEnvironmentString ("PATH")
  387.     
  388.     --Clear the 'Path=' at the beginning of the path list
  389.     get offset("=",currentPathList)
  390.     if it > 0 then
  391.         clear chars 1 to it of currentPathList
  392.     end if
  393.     
  394.     --Convert the path list to a list of items    
  395.     get offset(";",currentPathList)
  396.     while it > 0
  397.         set char it of currentPathList to ","
  398.         get offset(";",currentPathList)
  399.     end while
  400.     
  401.     --Search through the list, one path at a time
  402.     while currentPathList is not NULL
  403.         pop currentPathList into checkPath
  404.         
  405.         if last char of checkPath is not "\"
  406.             put "\" after checkPath
  407.         end if
  408.         -- we return the first place in the path where the file is found. 
  409.         if fileExists(checkPath & fileNameToCheck) = 1
  410.             unlinkDLL sysToolBookDirectory & "tb40dos.dll"
  411.             return checkPath
  412.         end if
  413.     end while
  414.     
  415.     --Unable to find file in the path
  416.     unlinkDLL sysToolBookDirectory & "tb40dos.dll"
  417.     return null
  418. end    fileInPath
  419. }
  420.  
  421. SCRIPT "Search for files"
  422. BEHAVIOR "Returns a list of files matching the supplied file name in the supplied directory"
  423. CATEGORY DOS{
  424.  --
  425.  -- This function returns a list of all files matching the supplied file name 
  426.  -- in the supplied starting directory tree. It requires the handler
  427.  -- searchDir below.
  428.  --
  429.  -- Parameters:
  430.  --
  431.  -- startingDir: a path in which the function searches for specified file(s).
  432.  -- All subdirectories below startingDir will also be searched.
  433.  --
  434.  -- fileName: a string containing the file name. Can contain wildcards
  435.  --
  436.  -- Returns a CRLF delimited list of files, or a negative number if
  437.  -- the startingDirectory supplied was invalid.
  438.  --
  439.  -- Example:
  440.  --
  441.  -- to get a list of all WAV files on your C drive:
  442.  -- get searchDrive("c:\","*.wav")
  443.  --
  444. to get searchDrive startingDir,fileName
  445.     system foundFiles,dirCount
  446.     clear foundFiles
  447.     dircount = 0
  448.     linkdll "tb40dos.dll"
  449.         INT    fileExists        (STRING)
  450.         STRING    getDirectoryOnlyList    (STRING,STRING)
  451.         STRING    getFileOnlyList        (STRING,STRING,STRING)
  452.         STRING    getFileAttributes    (STRING)    
  453.     end
  454.     -- get rid of trailing slash if path is not root:
  455.     if last char of startingDir is "\" and \
  456.      char (charcount(startingDir)-1) of startingDir is not ":"
  457.         clear last char of startingDir
  458.     end
  459.     -- check to see if startingDir is valid
  460.     test = getFileAttributes(startingDir)
  461.     if not (test contains "D")
  462.         retval = syserror
  463.     else
  464.         if last char of startingDir is not "\"
  465.             put "\" after startingDir
  466.         end
  467.         statusBarStatus = visible of statusbar
  468.         statusBarText = caption of statusBar
  469.         visible of statusbar = true
  470.         -- send this directory to be searched
  471.         send searchDir startingDir,FileName
  472.         visible of statusbar = statusBarStatus
  473.         caption of statusBar = statusBarText
  474.         retval = foundfiles
  475.         clear foundfiles
  476.         if retval is not null
  477.             clear last char of retval
  478.             clear last char of retval
  479.         end
  480.     end
  481.     clear dircount
  482.     unlinkdll function \
  483.      "fileExists,getDirectoryOnlyList,getFileOnlyList,getFileAttributes" \
  484.       from "tb40Dos.dll"
  485.     return retval
  486. end
  487.  
  488.  -- called by searchDrive and by itself. Accumulates list of all matching
  489.  -- files to filename.
  490. to handle searchDir dir,fileName
  491.     system foundFiles,dirCount
  492.     increment dircount
  493.     put dir into caption of statusBar
  494.     files = lowerCase(getFileOnlyList(dir&filename,null,"N"))
  495.     if files is not null
  496.         step i from 1 to textlinecount(files)
  497.             fn = dir&textline i of files
  498.             put fn & CRLF after foundFiles
  499.         end
  500.     end
  501.     -- send each directory in dir to be searched
  502.     dirs = lowerCase(getDirectoryOnlyList(dir,"N"))
  503.     step i from 1 to textlinecount(dirs)
  504.         curDir = textline i of dirs
  505.         if curDir is not "." and curDir is not ".."
  506.             send searchDir dir & curDir &"\",filename
  507.         end
  508.     end
  509. end
  510. }
  511.  
  512. SCRIPT "Get the center of an object"
  513. BEHAVIOR    "Gets the location of the center of an object."
  514. CATEGORIES    Utilities
  515. {
  516. to get objectCenter obj
  517.     if obj is null
  518.         set obj to target
  519.     end
  520.     get bounds of obj
  521.     set r to round(average (item 2 of it, item 4 of it))
  522.     push round(average (item 1 of it, item 3 of it)) onto r
  523.     return r
  524. end    objectCenter
  525. }
  526.  
  527. SCRIPT "Center an object at a location"
  528. BEHAVIOR    "Sets the center of an object to a particular location."
  529. CATEGORIES    Utilities
  530. {
  531. to set objectCenter to loc
  532.     get bounds of target
  533.     move target to (item 1 of loc - (item 3 of it - item 1 of it) div 2),\
  534.       (item 2 of loc - (item 4 of it - item 2 of it) div 2)
  535. end    objectCenter
  536. }
  537.  
  538. SCRIPT "Make full screen"
  539. BEHAVIOR "Without maximizing the screen, makes the application fit in the window, and its device independent"
  540. CATEGORIES Utilities
  541. ARG setOff oneOf "0,4" is "0" help "You can push the borders off the edge of the screen by setting this to 4, or leave them alone by accepting the default."
  542. {
  543. to handle enterApplication
  544.     linkDLL "tb40win.dll"
  545.         INT horizontalDisplayRes()
  546.         INT verticalDisplayRes()
  547.     end
  548.     set position of mainWindow to -$$setOff,-$$setOff
  549.     set maximumSize of mainWindow to\
  550.         horizontalDisplayRes() + (2 * $$setOff),verticalDisplayRes() + (2 * $$setOff)
  551.     set minimumSize of mainWindow to\
  552.         horizontalDisplayRes() + (2 * $$setOff),verticalDisplayRes() + (2 * $$setOff)
  553.     unlinkDLL "tb40Win.dll"
  554.     
  555. end
  556. }